package com.assa.jco.handler.simplehandler;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.assa.jco.handler.AbstractFunctionHandler;
import com.sap.conn.jco.JCoField;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoRecordFieldIterator;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;
import com.sap.conn.jco.server.JCoServerContext;

public class Z_PO_BAM_PEDIDO_Handler extends AbstractFunctionHandler {

	private static Log logger = LogFactory.getLog(Z_PO_BAM_PEDIDO_Handler.class);
	
	@Override
	protected Log getLog() {
		return logger;
	}

	@Override
	protected void logExtraInfo(JCoServerContext serverCtx, JCoFunction function) {
		getLog().debug("----------------------------------------------------------------");
		getLog().debug("Exports EKKO: ");
		JCoStructure ekko = function.getImportParameterList().getStructure("IM_EKKO");
		
		JCoRecordFieldIterator iterator2 = ekko.getRecordFieldIterator();

		while (iterator2.hasNextField()) {
			JCoField field = iterator2.nextField();
			getLog().debug("Field :" + field.getName() + " tipo: " + field.getTypeAsString());
		}		
	}

	@Override
	protected Map<String, String> processRequest(JCoServerContext serverCtx, JCoFunction function) {
		JCoStructure ekko = function.getImportParameterList().getStructure("IM_EKKO");
		JCoTable ekpo = function.getImportParameterList().getTable("IM_EKPO");

		Map<String, String> ekkoStructure =  loadStructure(ekko, "EKKO");
		List<Map<String,String>> ekpoTable = loadTable(ekpo, "EKPO");
		
		function.getExportParameterList().setValue("RETCODE", 0);
		
		//Seteo la cantidad de articulos vendidos
		ekkoStructure.put("cantidad", Integer.toString(ekpoTable.size()));
	
		return ekkoStructure;
	}

}
